package com.example.manager.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.manager.common.QueryPageParam;
import com.example.manager.common.Result;
import com.example.manager.entity.Employees;
import com.example.manager.entity.Employeestatus;
import com.example.manager.entity.Users;
import com.example.manager.service.EmployeesService;
import com.example.manager.service.EmployeestatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fwy
 * @since 2024-07-30
 */
@RestController
@RequestMapping("/employeestatus")
public class EmployeestatusController {
    @Autowired
    private EmployeestatusService employeestatusService;
    @Autowired
    private EmployeesService employeesService;

    //查询
    @GetMapping("/list")
    public List<Employeestatus> list(){
        return employeestatusService.list();
    }

//    //编辑
//    @PostMapping("/mod")
//    public Result mod(@RequestBody Employeestatus employeestatus) {
//        boolean success = employeestatusService.updateById(employeestatus);
//        return success ? Result.suc(employeestatus) : Result.fail();
//    }

    @PostMapping("/mod")
    public Result mod(@RequestBody Employeestatus employeestatus) {
        // 检查传入的Employeestatus对象是否包含有效的ID
        if (employeestatus.getId() == null) {
            return Result.fail();
        }

        try {
            // 首先，更新Employeestatus表
            boolean statusUpdated = employeestatusService.updateById(employeestatus);
            if (!statusUpdated) {
                return Result.fail();
            }
            // 接着，根据Employeestatus中的信息更新Employees表
            // 假设employeestatus对象中包含了所有需要更新到Employees表的字段信息
            Employees employee = new Employees();
            // 此处需要根据实际情况赋值，以下仅为示例
          // 设置Employees表的主键
            employee.setId(employeestatus.getId());
            employee.setWorkLocation(employeestatus.getWorkLocation());
            employee.setDepartment(employeestatus.getDepartment());
            employee.setJobLevel(employeestatus.getJobLevel());
            employee.setJobSeries(employeestatus.getPosition());
            employee.setHireDate(employeestatus.getJoiningDate());


            boolean employeeUpdated = employeesService.updateById(employee);
            if (!employeeUpdated) {
                return Result.fail();
            }

            // 如果两个更新都成功，返回成功结果
            return Result.suc(employeestatus);

        } catch (Exception e) {
            // 记录异常信息，返回失败结果
            e.printStackTrace();
            return Result.fail();
        }
    }

    //删除
    @DeleteMapping("/del")
    public Result delete(@RequestParam String id) {
        return  employeestatusService.removeById(id) ? Result.suc() : Result.fail();
    }

    @PostMapping("/listPage1")
    public Result listPage1(@RequestBody QueryPageParam query){
        System.out.println(query);
        HashMap param=query.getParam();
        String id =(String)param.get("id");

        Page<Employeestatus> page=new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Employeestatus> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(id)) {
            lambdaQueryWrapper.eq(Employeestatus::getId, id); // 确保使用了正确的字段名称
        }
        IPage result=employeestatusService.pageCC(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());}
}
